<?php
	//set_magic_quotes_runtime(0);
	
	if(function_exists('date_default_timezone_set'))
		date_default_timezone_set('PRC');

	define('BASEDIR',getDirName(__FILE__));
	define('ROOTDIR',BASEDIR."../");
	define('FANWE_ROOT',ROOTDIR);	
	
	require_once ROOTDIR."classes/qqmail.class.php" ;	
	require_once ROOTDIR.'classes/dbconfig.php';
	require_once ROOTDIR.'classes/excel.class.php';	
	require_once ROOTDIR.'classes/db/promotion.db.php';
	
	define('APPKEY',"12490401");	// 12511402
	define('SECRET',"6374d7123c38d814bc4abaaea2565426");	//	 2d1d7389b69b5f95d52a58ee58ab3528
	
	$db_reader= new DB(MGR_HOST_R,MGR_USER_R,MGR_PASS_R,MGR_NAME_R,MGR_CHARSET_R,0);
	$db_writer = new DB(MGR_HOST_W,MGR_USER_W,MGR_PASS_W,MGR_NAME_W,MGR_CHARSET_W,0);	
	
	function getDirName($path=null){
		if (!empty($path)) {
			if (strpos($path,'\\')!==false) {
				return substr($path,0,strrpos($path,'\\')).'/';
			} elseif (strpos($path,'/')!==false) {
				return substr($path,0,strrpos($path,'/')).'/';
			}
		}
		return './';
	}
	
	/*
	getrealurl 获取301、302重定向后的URL地址
	@param str $url 查询
	$return str  定向后的url的真实url
	*/
	function getRealUrl($url){
		$header = get_headers($url,1);
		if (strpos($header[0],'301') || strpos($header[0],'302')) {
			if(is_array($header['Location'])) {
				return $header['Location'][count($header['Location'])-1];
			}else{
				return $header['Location'];
			}
		}else {
			return $url;
		}
	}
	
	function getRealUrlReferer($url,$referer){
		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5");
		curl_setopt($ch,CURLOPT_REFERER,$referer);		
		$strRes=curl_exec($ch);
		curl_close($ch);
		return $strRes;
	}
	
	function encodeBaiduShortUrl($url){
		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL,"http://dwz.cn/create.php");
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		$data=array('url'=>$url);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		$strRes=curl_exec($ch);
		curl_close($ch);
		$arrResponse=json_decode($strRes,true);
		if($arrResponse['status']!=0){
			/**错误处理*/
			echo iconv('UTF-8','GBK',$arrResponse['err_msg'])."\n";
			return null;
		}
		return $arrResponse['tinyurl'];		
	}
	
	function decodeBaiduShortUrl($tinyurl){
		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL,"http://dwz.cn/query.php");
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		$data=array('tinyurl'=>$tinyurl);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		$strRes=curl_exec($ch);
		curl_close($ch);
		$arrResponse=json_decode($strRes,true);
		if($arrResponse['status']!=0){
			/**错误处理*/
			echo iconv('UTF-8','GBK',$arrResponse['err_msg'])."\n";
			return null;
		}
		return $arrResponse['longurl'];
	}
	
	function decodeSinaShortUrl($url){
		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL,"http://jucelin.com/lab/short.php?type=2&url=".$url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
		//设定返回数据是否自动显示
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		//设定是否显示头信息
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
		$strRes=curl_exec($ch);
		curl_close($ch);
		return $strRes;
	}
	
	function encodeSinaShortUrl($url){
		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL,"https://api.weibo.com/2/short_url/shorten.json?source=1681459862&url_long=".urlencode($url));
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
		//设定返回数据是否自动显示
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		//设定是否显示头信息
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
		$strRes=curl_exec($ch);
		curl_close($ch);
		$arrResponse=json_decode($strRes);		
		return $arrResponse->urls[0]->url_short;
	}
	
	function fiterHtmlTags($filename,$tag) {
		$buffer = join("",file($filename));
		$buffer = eregi_replace("\r\n","",$buffer);
		$tagkey = sql_regcase($tag);
		$buffer = eregi_replace("<$tagkey ","\n<$tag ",$buffer);
		$ar = split("\n",$buffer);
	
		foreach($ar as $v) {
			if(! eregi("<$tagkey ",$v)) continue;
			eregi("<$tagkey ([^>]*)((.*)</$tagkey)?",$v,$regs);
			$p[tagName] = strtoupper($tag);
			if($regs[3])
				$p[Text] = $regs[3];
			$s = trim(eregi_replace("[ \t]+"," ",$regs[1]))." ";
			$s = eregi_replace(" *= *","=",$s);
	
			$a = split(" ",$s);
			for($i=0;$i<count($a);$i++) {
				$ch = array();
				if(eregi("=[\"']",$a[$i])) {
					$j = $i+1;
					while(!eregi("[\"']$",$a[$i])) {
						$a[$i] .= " ".$a[$j];
						unset($a[$j]);
					}
				}
			}
			foreach($a as $k) {
				$name = strtoupper(strtok($k,"="));
				$value = strtok("\0");
				if(eregi("^[\"']",$value))
					$value = substr($value,1,-1);
				if($name)
					$p[Attrs][$name] = $value;
			}
			$pp[] = $p;
		}
		return $pp;
	}	
?>